import random
from common.handlerequest import SendRequest
from common.handleconfig import conf
import jsonpath
from common.handledata import CaseData, replace_data, GenerateDate

import datetime




def generate_data(str1,str2="",site = "r"):
    date = datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")
    if site == "r":
        s = str(str1) + date + str(str2)
    elif site == "l":
        s = date + str(str1)
    else:
        s = str(str1)

    return s

def generate_random_str(len):
    str = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-"
    try:
        len = int(len)
    except:
        return
    else:
        s = []
        for i in range(len):
            s.append(random.choice(str))
        return "".join(s)
# s = generate_random_str(201)

g = GenerateDate()
i = conf.get("test_data", "name")
print(i)

over50len = g.generate_random_str(51)
print(over50len)
s = g.generate_data(conf.get("test_data", "name"))
print(s)


# print(s)
#
# {"name": "模拟番茄种植项目","leader": "Lee","tester": "Tina","programmer": "Kelly","publish_app": "番茄种植","desc": ""}
#
{"name": ["该字段不能为空。"],"interface": {"non_field_errors": ["无效数据。期待为字典类型，得到的是 str 。"]},"include": ["该字段不能为空。"],"author": ["该字段不能为空。"],"request": ["该字段不能为空。"]}


def getdata():
    url = conf.get("evn", "url") + "/member/login"
    headers = eval(conf.get("evn", "headers"))
    login_data = {"mobile_phone": "15912345679", "pwd": "12345678"}
    request = SendRequest()
    response = request.send_request(url=url, headers=headers, json=login_data, method="post")
    member_id = jsonpath.jsonpath(response.json(), "$..id")[0]
    token = jsonpath.jsonpath(response.json(), "$..token_type")[0] + " " + jsonpath.jsonpath(response.json(), "$..token")[0]
    return member_id,token

{"interface": {"iid": ["请填写合法的整数值。"],"pid": ["请填写合法的整数值。"]}}


"""
print(getdata()[0])
print(getdata()[1])


def phone():
    phone = "136"
    n = random.randint(10000000, 99999999)
    phone += str(n)

    return phone



n = phone()
m = {"mobile_phone":"#phone#","pwd":"12345678","reg_name":"abcdef1234","type":"1"}
m["mobile_phone"] = m["mobile_phone"].replace("#phone#",n)
print(m)



d = "2020-02-29 16:26:17.0".split('.')[0]
# print(datetime.datetime(int(d.split('.')[0])))
d2= "2020, 2, 29, 16, 26, 17"
s=datetime.strptime(d,'%Y-%m-%d %H:%M:%S')
print(s)
"""




